package TangramBase;

import TangramCore.HladinaObrys;
import TangramCore.Obrazec;
import TangramCore.Vzhled;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import javax.swing.JColorChooser;
import javax.swing.JDialog;

public class StyleDialog extends JDialog {
	static final long serialVersionUID = 24362462L;
	
    JColorChooser dlgColor;
    private Vzhled vzhled;
    private HladinaObrys hladinaObrys;
    private Obrazec obrazec;
    
    public StyleDialog(Obrazec obr, Frame parent, boolean modal) {
        super(parent, modal);
        obrazec = obr;
        initComponents();
        addWindowListener(new java.awt.event.WindowAdapter() {public void windowClosing(WindowEvent e){cmdCancel.doClick();}});
        //dlgColor = JColorChooser.createDialog(this, "Select color", true, null, null, null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panVzhled = new javax.swing.JPanel();
        panFillColor = new javax.swing.JPanel();
        lblCara = new javax.swing.JLabel();
        lblPruhlednost = new javax.swing.JLabel();
        sldPruhlednost = new javax.swing.JSlider();
        sldSilaCary = new javax.swing.JSlider();
        panLineColor = new javax.swing.JPanel();
        txtSilaCary = new javax.swing.JFormattedTextField();
        txtPruhlednost = new javax.swing.JFormattedTextField();
        cmdOK = new javax.swing.JButton();
        cmdCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Style editor");

        panVzhled.setBorder(javax.swing.BorderFactory.createTitledBorder("Style"));

        panFillColor.setBackground(new java.awt.Color(255, 0, 51));
        panFillColor.setPreferredSize(new java.awt.Dimension(32, 32));
        panFillColor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                panColorMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout panFillColorLayout = new javax.swing.GroupLayout(panFillColor);
        panFillColor.setLayout(panFillColorLayout);
        panFillColorLayout.setHorizontalGroup(
            panFillColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 52, Short.MAX_VALUE)
        );
        panFillColorLayout.setVerticalGroup(
            panFillColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 53, Short.MAX_VALUE)
        );

        lblCara.setText("Line width");

        lblPruhlednost.setText("Fill transparency");

        sldPruhlednost.setMajorTickSpacing(25);
        sldPruhlednost.setMinorTickSpacing(5);
        sldPruhlednost.setPaintTicks(true);
        sldPruhlednost.setValue(0);

        sldSilaCary.setMajorTickSpacing(25);
        sldSilaCary.setMinorTickSpacing(5);
        sldSilaCary.setPaintTicks(true);
        sldSilaCary.setValue(0);

        panLineColor.setBackground(new java.awt.Color(0, 0, 0));
        panLineColor.setPreferredSize(new java.awt.Dimension(32, 32));
        panLineColor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                panColorMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout panLineColorLayout = new javax.swing.GroupLayout(panLineColor);
        panLineColor.setLayout(panLineColorLayout);
        panLineColorLayout.setHorizontalGroup(
            panLineColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 52, Short.MAX_VALUE)
        );
        panLineColorLayout.setVerticalGroup(
            panLineColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 53, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout panVzhledLayout = new javax.swing.GroupLayout(panVzhled);
        panVzhled.setLayout(panVzhledLayout);
        panVzhledLayout.setHorizontalGroup(
            panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panVzhledLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panLineColor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE)
                    .addComponent(panFillColor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPruhlednost)
                    .addComponent(lblCara)
                    .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(txtPruhlednost, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(panVzhledLayout.createSequentialGroup()
                            .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(sldPruhlednost, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(sldSilaCary, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtSilaCary, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        panVzhledLayout.setVerticalGroup(
            panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panVzhledLayout.createSequentialGroup()
                .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panLineColor, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                    .addGroup(panVzhledLayout.createSequentialGroup()
                        .addComponent(lblCara)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSilaCary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sldSilaCary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panFillColor, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panVzhledLayout.createSequentialGroup()
                        .addComponent(lblPruhlednost)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(panVzhledLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPruhlednost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sldPruhlednost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        cmdOK.setText("OK");
        cmdOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOKActionPerformed(evt);
            }
        });

        cmdCancel.setText("Cancel");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panVzhled, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(194, Short.MAX_VALUE)
                .addComponent(cmdOK)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdCancel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panVzhled, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdOK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void panColorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panColorMouseClicked
        Component pan = evt.getComponent();
        String title = pan == panLineColor ? "Line color" : "Fill color";
        pan.setBackground(JColorChooser.showDialog(this, title, pan.getBackground()));
}//GEN-LAST:event_panColorMouseClicked

    private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOKActionPerformed
        vzhled.setLineWidth(sldSilaCary.getValue() / 100.0f * MAX_SILA);
        vzhled.setLineColor(panLineColor.getBackground());
        Color c = panFillColor.getBackground();
        int alpha = (int)((100 - sldPruhlednost.getValue()) / 100.0f * 255);
        vzhled.setFillColor(new Color(c.getRed(), c.getGreen(), c.getBlue(),alpha));
        hladinaObrys.setVzhled(vzhled);
        obrazec.update();
        setVisible(false);
    }//GEN-LAST:event_cmdOKActionPerformed

    private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cmdCancelActionPerformed

    private static final float MAX_SILA = 1.0f;
    public void show(HladinaObrys ho){
        hladinaObrys = ho;
        vzhled = ho.getVzhled();
        Color c = vzhled.getLineColor();
        panLineColor.setBackground(c == null ? Color.MAGENTA : c);
        c = vzhled.getFillColor();
        panFillColor.setBackground(c == null ? Color.MAGENTA : c);
        sldPruhlednost.setValue(c == null ? 100 : (int)(100.0f*(255-c.getAlpha())/255));
        sldSilaCary.setValue((int)(100.0f*vzhled.getLineWidth()/MAX_SILA));
        setVisible(true);
    }
     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdOK;
    private javax.swing.JLabel lblCara;
    private javax.swing.JLabel lblPruhlednost;
    private javax.swing.JPanel panFillColor;
    private javax.swing.JPanel panLineColor;
    private javax.swing.JPanel panVzhled;
    private javax.swing.JSlider sldPruhlednost;
    private javax.swing.JSlider sldSilaCary;
    private javax.swing.JFormattedTextField txtPruhlednost;
    private javax.swing.JFormattedTextField txtSilaCary;
    // End of variables declaration//GEN-END:variables
    
}
